<?php

/* * ***************************************************
 * Rapidleech 36B Rv.8
 *
 * $Id: index.php - 05apr2010-Idx $
 * *************************************************** */

define('RAPIDLEECH', 'yes');
define('THIS_SCRIPT', basename(__FILE__));

require_once "./global.php";


define('BUILD', '23082007');
define('CREDITS', '<a href="http://www.rapidleech.com/" style="text-decoration:none"><b>RL</b></a>&nbsp;<b class="footer1">PlugMod rev. ' . $rev_num . '</b> <span class="footer2">' . $dev_name . '</span><br /><small class="footer2">Credits to Pramode &amp; Checkmate &amp; Kloon. Mod by: MsNeil &amp; Idoenk</small>');
define('VERSION', '<small class="footer3"><span>[ olc | ccpb | kaskus ]</span><br />[ Shout: Viant77, Jujuan, Huka, Limpy ]</small>');


//************************
// MAIN ...
//************************

foreach ($_POST as $key => $value) {
    $_GET[$key] = $value;
}

if (!$_COOKIE && isset($_SERVER["HTTP_COOKIE"])) {
    if (strstr($_SERVER["HTTP_COOKIE"], ";")) {
        foreach (explode("; ", $_SERVER["HTTP_COOKIE"]) as $key => $value) {
            list($var, $val) = explode("=", $value);
            $_COOKIE[$var] = $val;
        }
    } else {
        list($var, $val) = @explode("=", $_SERVER["HTTP_COOKIE"]);
        $_COOKIE[$var] = $val;
    }
}

require_once(CLASS_DIR . "cookie.php");

if (!@file_exists(HOST_DIR . "download/hosts.php")) {
    create_hosts_file("download/hosts.php");
}

if (!empty($_GET["image"])) {
    require_once(CLASS_DIR . "http.php");
    require_once(CLASS_DIR . "image.php");
    exit;
}

if (isset($_GET["useproxy"]) && (!$_GET["proxy"] || !strstr($_GET["proxy"], ":"))) {
    html_error($L->say['wrong_proxy']);
} else {
    if (isset($_GET["pauth"])) {
        $pauth = $_GET["pauth"];
    } else {
        $pauth = (isset($_GET["proxyuser"]) && isset($_GET["proxypass"])) ? base64_encode($_GET["proxyuser"] . ":" . $_GET["proxypass"]) : "";
    }
}

if (!isset($_GET["path"]) || $options["download_dir_is_changeable"] == false) {
    if (!isset($_GET["host"])) {
        $_GET["path"] = (substr($options["download_dir"], 0, 6) != "ftp://") ? realpath(DOWNLOAD_DIR) : $options["download_dir"];
    } else {
        $_GET["saveto"] = (substr($options["download_dir"], 0, 6) != "ftp://") ? realpath(DOWNLOAD_DIR) : $options["download_dir"];
    }
}

if (!isset($_GET["filename"]) || !$_GET["host"] || !$_GET["path"]) {
    //require "host.php";
    //require_once(HOST_DIR."hosts.php");
    require_once(HOST_DIR . "download/hosts.php");

    if (!isset($_POST["link"])) {
        if (isset($_GET["idx"])) {
            // link was sent from audl
            $LINK = (isset($_GET["link"]) ? utf8_strrev(base64_decode($_GET["link"])) : "");
        } else {
            $LINK = (isset($_GET["link"]) ? $_GET["link"] : "");
        }
    } else {
        $LINK = $_POST["link"];
    }

    // urldecode, trim, strip * in link
    $LINK = urlcleaner(trim(urldecode($LINK)));

    if (!$LINK) {
        require_once(CLASS_DIR . "main.php");
        exit();
    } elseif ($limitation_alert === true) {
        html_error('Limitation alerted, can not use RL! ' . $msg, 1);
    }

    // Detect if it doesn't have a protocol assigned
    if (substr($LINK, 0, 7) != "http://" && substr($LINK, 0, 6) != "ftp://" && substr($LINK, 0, 6) != "ssl://" && substr($LINK, 0, 8) != "https://" && !stristr($LINK, '://')) {
        // Automatically assign http://
        $LINK = "http://" . $LINK;
    }

    if (!empty($_GET["saveto"]) && !$_GET["path"]) {
        html_error($L->say['path_not_defined']);
    }

    if (empty($_GET["useproxy"])) {
        $_GET["proxy"] = "";
    }

    if (!empty($_GET["domail"]) && !checkmail($_GET["email"])) {
        html_error($L->say['didnt_enter_mail']);
        if ($_GET["split"] && !is_numeric($_GET["partSize"])) {
            html_error($L->say['size_not_true']);
        }
    }

    $Referer = ($_GET["referer"] ? trim(urldecode($_GET["referer"])) : $LINK);
    $Url = parse_url($LINK);
    if ($Url['scheme'] != 'http' && $Url['scheme'] != 'https' && $Url['scheme'] != 'ftp') {
        $LINK = urlcleaner(utf8_strrev(base64_decode($LINK)));
        $Url = parse_url($LINK);
        if ($Url['scheme'] != 'http' && $Url['scheme'] != 'https' && $Url['scheme'] != 'ftp') {
            html_error($L->say['url_unknown'] . ", <span style=color:#000>" .
                    $L->say['url_only_use'] . " <span style=color:#05F>http</span> " .
                    $L->say['url_or'] . " <span style=color:#05F>https</span> " .
                    $L->say['url_or'] . " <span style=color:#05F>ftp</span> Protocol</span>");
        }
    }

    /*
      if($options["pointboost"]>0){
      $pos = str_replace(".", "", strrchr($LINK, "."));
      $numidx = (is_numeric($pos) ? $pos : -1);
      }
     */

    if (isset($_GET['user_pass']) && $_GET['user_pass'] == "on") {
        $Url['user'] = $_GET['iuser'];
        $Url['pass'] = $_GET['ipass'];
        // Rebuild url
        $query = "";
        if ($Url['query']) {
            $query = '?' . $Url['query'];
        }
        $LINK = $Url['scheme'] . "://" . $Url['user'] . ":" . $Url['pass'] . "@" . $Url['host'] . $Url['path'] . $query;
    }

    $litehead = 1;
    if ($_GET["dis_plug"] != "on") {
        //check Domain-Host
        if (isset($_GET["vBulletin_plug"])) {
            $page_title = $L->say['downloading'];
            include(TEMPLATE_DIR . '/header.php');
            require_once(CLASS_DIR . "http.php");
            require_once(HOST_DIR . "download/vBulletin_plug.php");
            exit;
        } else {
            foreach ($host as $site => $file) {
                if (preg_match("/^(.+\.)?" . $site . "$/i", $Url["host"])) {
                    $page_title = $L->say['prep_dl'] . " " . $LINK;
                    $shortcut_icon = ROOT_URL . IMAGE_DIR . "rsload_2.gif";
                    include(TEMPLATE_DIR . '/header.php');
                    require_once(CLASS_DIR . "http.php");
                    require_once (HOST_DIR . "DownloadClass.php");

                    require_once (HOST_DIR . 'download/' . $file);
                    $class = substr($file, 0, -4);
                    $firstchar = substr($file, 0, 1);
                    if ($firstchar > 0) {
                        $class = "d" . $class;
                    }
                    if (class_exists($class)) {
                        $hostClass = new $class();
                        $hostClass->Download($LINK);
                    }
                    exit ();
                }
            }
        }
    }

    $page_title = $L->say['leeching'];
    $shortcut_icon = ROOT_URL . IMAGE_DIR . "rsload_2.gif";
    include(TEMPLATE_DIR . '/header.php');

    $Url = parse_url($LINK);
    $FileName = basename($Url["path"]);
    $mydomain = $_SERVER['SERVER_NAME'];
    if (!$options['bw_save'] && preg_match("/($mydomain|$myip)/i", $Url["host"])) {
        html_error("You are not allowed to leech from <font color=black>" . $mydomain . " (" . $ipmu . ")</font>");
    }

    $auth = ($Url ["user"] && $Url ["pass"]) ? "&auth=" . urlencode(encrypt(base64_encode($Url ["user"] . ":" . $Url ["pass"]))) : "";

    if (isset($_GET ['cookieuse'])) {
        if (strlen($_GET ['cookie'] > 0)) {
            $_GET ['cookie'] .= ';' . $_POST ['cookie'];
        } else {
            $_GET ['cookie'] = $_POST ['cookie'];
        }
    }
    insert_location("$PHP_SELF?filename=" . urlencode($FileName) . "&host=" . $Url["host"] . "&port=" . $Url["port"] . "&path=" . urlencode($Url["path"] . ($Url["query"] ? "?" . $Url["query"] : "")) . "&referer=" . urlencode($Referer) . "&email=" . ($_GET["domail"] ? $_GET["email"] : "") . "&partSize=" . ($_GET["split"] ? $_GET["partSize"] : "") . "&method=" . $_GET["method"] . "&proxy=" . ($_GET["useproxy"] ? $_GET["proxy"] : "") . "&saveto=" . $_GET["path"] . "&link=" . urlencode($LINK) . ($_GET["add_comment"] == "on" ? "&comment=" . urlencode($_GET["comment"]) : "") . $auth . ($pauth ? "&pauth=$pauth" : "") . (isset($_GET["idx"]) ? "&idx=" . $_GET["idx"] : "") . "&cookie=" . urlencode(encrypt($_GET ['cookie'])));
} else {
    $page_title = 'nYedoTz...';
    $shortcut_icon = ROOT_URL . IMAGE_DIR . "rsload_2.gif";
    $litehead = 1;
    include(TEMPLATE_DIR . '/header.php');
    echo('<div align="center">');

    if ($options["limitbyip"]) {
        if ($ada_acc) {
            $trheute = $heute + 1;
            if ($trheute > $options["maximum_free_downloads"]) {
                html_error($msgNya);
            }
        }
    }
    do {
        list($_GET["filename"], $tmp) = explode('?', urldecode(trim($_GET["filename"])));
        $_GET["saveto"] = urldecode(trim($_GET["saveto"]));
        $_GET["host"] = urldecode(trim($_GET["host"]));
        $_GET["path"] = urldecode(trim($_GET["path"]));
        $_GET["port"] = $_GET["port"] ? urldecode(trim($_GET["port"])) : 80;
        $_GET["referer"] = $_GET["referer"] ? urldecode(trim($_GET["referer"])) : 0;
        $_GET["link"] = urldecode(trim($_GET["link"]));

        $_GET["post"] = $_GET["post"] ? unserialize(stripslashes(urldecode(trim($_GET["post"])))) : 0;
        $_GET ["cookie"] = $_GET ["cookie"] ? decrypt(urldecode(trim($_GET["cookie"]))) : "";

        $redirectto = "";

        $pauth = urldecode(trim($_GET["pauth"]));
        $auth = urldecode(trim($_GET["auth"]));

        if ($_GET['auth'] == 1) {
            if (!preg_match("|^(?:.+\.)?(.+\..+)$|i", $_GET ["host"], $hostmatch))
                html_error('No valid hostname found for authorisation!');
            $hostmatch = str_replace('.', '_', $hostmatch[1]);
            if ($premium_acc ["$hostmatch"] && $premium_acc ["$hostmatch"] ["user"] && $premium_acc ["$hostmatch"] ["pass"]) {
                $auth = base64_encode($premium_acc ["$hostmatch"] ["user"] . ":" . $premium_acc ["$hostmatch"] ["pass"]);
            }
            else
                html_error('No useable premium account found for this download - please set one in accounts.php');
        }
        else {
            $auth = decrypt(urldecode(trim($_GET['auth'])));
        }

        if ($_GET["auth"]) {
            $AUTH["use"] = TRUE;
            $AUTH["str"] = $_GET["auth"];
        } else {
            unset($AUTH);
        }

        $ftp = parse_url($_GET["link"]);

        $IS_FTP = $ftp["scheme"] == "ftp" ? TRUE : FALSE;
        $AUTH["ftp"] = array("login" => $ftp["user"] ? $ftp["user"] : "anonymous",
            "password" => $ftp["pass"] ? $ftp["pass"] : "anonymous@leechget.com");

        $pathWithName = $_GET["saveto"] . PATH_SPLITTER . $_GET["filename"];
        while (stristr($pathWithName, "\\\\")) {
            $pathWithName = str_replace("\\\\", "\\", $pathWithName);
        }

        list($pathWithName, $tmp) = explode('?', $pathWithName);

        if ($ftp["scheme"] == "ftp" && !$_GET["proxy"]) {
            require_once(CLASS_DIR . "ftp.php");
            $file = getftpurl($_GET["host"], $ftp["port"] ? $ftp["port"] : 21, $_GET["path"], $pathWithName);
        } else {
            require_once(CLASS_DIR . "http.php");
            $_GET["force_name"] ? $force_name = urldecode($_GET["force_name"]) : '';
            $file = geturl($_GET["host"], $_GET["port"], $_GET["path"], $_GET["referer"], $_GET["cookie"], $_GET["post"], $pathWithName, $_GET["proxy"], $pauth, $auth, $ftp["scheme"]);
        }
        if ($options['redir'] && $lastError && stristr($lastError, $L->say['_error'] . " " . $L->say['_redirect_to'] . " [")) {
            $redirectto = trim(cut_str($lastError, $L->say['_error'] . " " . $L->say['_redirect_to'] . " [", "]"));
            print $L->say['_redirecting_to'] . " <b>$redirectto</b> ... <br />$nn";
            $_GET ["referer"] = $_GET ["link"];
            $_GET["link"] = $redirectto;
            $purl = parse_url($redirectto);
            list($_GET["filename"], $tmp) = explode('?', basename($redirectto));

            // In case the redirect didn't include the host
            $_GET ["host"] = ($purl ["host"]) ? $purl ["host"] : $_GET ["host"];
            $_GET ["path"] = $purl ["path"] . ($purl ["query"] ? "?" . $purl ["query"] : "");
            $_GET ['port'] = $purl ['port'] ? $purl ['port'] : 80;
            $_GET ['cookie'] = $_GET ["cookie"] ? urlencode(encrypt($_GET["cookie"])) : "";
            $lastError = "";
        }
    } while ($redirectto && !$lastError);


    if ($lastError) {
        html_error($lastError, 0);
    } elseif ($file["bytesReceived"] == $file["bytesTotal"] || $file["size"] == "Unknown") {
        $inCurrDir = stristr(dirname($pathWithName), ROOT_DIR) ? TRUE : FALSE;
        if ($inCurrDir) {
            $Path = parse_url($PHP_SELF);
            $Path = substr($Path["path"], 0, strlen($Path["path"]) - strlen(strrchr($Path["path"], "/")));
            $time = @filemtime(substr(dirname($pathWithName), strlen(ROOT_DIR)) . "/" . basename($file["file"]));
            $final_link = $Path . "/" . substr(dirname($pathWithName), strlen(ROOT_DIR)) . "/" . basename($file["file"]);
        } else {
            $cl_Path = relink_path(DOWNLOAD_DIR, $PHP_SELF);
            $time = @filemtime(DOWNLOAD_DIR . basename($file["file"]));
            $final_link = $cl_Path["root"] . "/" . $cl_Path["download"] . basename($file["file"]);
        }


        //$unix_zone = ( $unix_zone - date("Z") + (3600 * $options["timezone"]));
        $file["date"] = getNowzone($time);

        $tosave = array("name" => $file["file"], "size" => str_replace(" ", "&nbsp;", $file["size"]), "date" => $file["date"], "age" => $L->say['less_a_minute'], "link" => $_GET["link"], "comment" => str_replace("\n", "\\n", str_replace("\r", "\\r", $_GET["comment"])), "ip" => $ipmu);
        //print_r($tosave);
        $result = write_file(FILES_LST, serialize($tosave) . "\r\n", 0);

        $result2 = write_file(IP_L33CH_L0G, serialize(array("name" => $file["file"], "date" => $file["date"], "ip" => $ipmu)) . "\r\n", 0);

        if (!$result || !$result2) {
            echo $L->say['error_upd_list'] . "<br />";
        } else {
            $rnd = rand(11, 99);
            echo "<script type=\"text/javascript\">pr(100, '" . $file["size"] . "', '" . $file["speed"] . "')</script>";
            echo $L->sprintf($L->say['_filesaved'], '<a href="' . $final_link . '">' . basename($file["file"]) . '</a>', $file ["size"], $file ["time"], $file ["speed"]);
            // generate delete link
            echo $L->sprintf($L->say['_del_link'], 'del.php?d=' . str_replace("=", "", rotN(base64_encode($file["date"] . ':' . '4puZ'), $rnd)) . "-" . $rnd, date("M-d, Y - H:i:s", $file["date"]));
        }

        if ($options["limitbytraffic"]) {
            if ($options["max_trafic"] > 0) {
                if (!is_numeric($traffic->total))
                    $traffic->total = 0;
                $traffic->total += $file["bytesReceived"];
                $traffic->updTraffic();
            }
        }


        if ($_GET["email"]) {
            require_once(CLASS_DIR . "mail.php");
            $_GET["partSize"] = (isset($_GET["partSize"]) ? $_GET["partSize"] * 1024 * 1024 : FALSE);
            if (xmail($fromaddr, $_GET["email"], "File " . basename($file["file"]), "File: " . basename($file["file"]) . "\r\n" . "Link: " . $_GET["link"] . ($_GET["comment"] ? "\r\n" . "Comments: " . str_replace("\\r\\n", "\r\n", $_GET["comment"]) : ""), $pathWithName, $_GET["partSize"], $_GET["method"])) {
                print "<script type=\"text/javascript\">mail('" . $L->say['mail_file_sent'] . "<b>" . $_GET["email"] . "</b>.', '" . basename($file["file"]) . "');</script>";
            } else {
                print "<div class='acthistory_result'>" . $L->say['mail_error_send'] . "</div>";
            }
        }

        /*
          // restrict loop if this index called by audl.php; (empty($_GET["idx"]))
          if($options["pointboost"]>0 && empty($_GET["idx"])){
          // i'd rather not delete current downloaded file
          if(!defined("DOWNLOADED_FILE")){define('DOWNLOADED_FILE', DOWNLOAD_DIR.basename($file["file"]));}
          @write_file(DOWNLOADED_FILE, " ");
          if($numidx < $options["pointboost"]-1)
          {
          // generate reload form (link, secondcounterdown, auth, indexnumber)
          echo genReload($_GET["link"], 3, $auth, $numidx);
          }
          }
         */

        print "<br /><a id='tdone' href=\"" . $PHP_SELF . "\">[&nbsp;<b><span id='txtdone'>" . $L->say['back_main'] . "&nbsp;]</b></span></a><br />";
    } else {
        @unlink($pathWithName);
        print $L->say['con_lost'] . "<br /><a href=\"javascript:location.reload();\">" . $L->say['_reload'] . "</a>";
        print '<script type="text/javascript">setTimeout("location.reload()",1000);</script>';
    }
    //check whether this index called from audl.php
    $audlparam = (isset($_GET["idx"])) ? $_GET["idx"] : '';
    if ($audlparam != '') {
        $audlparam = explode('|', $audlparam);
        $audlparam[0] = ($audlparam[0] != '' ? true : false);
        if ($audlparam[1] == '') {
            $audlparam[1] = 'none';
        }
        echo autoNext($audlparam[0], true, $audlparam[1]);
    }
    echo ('</div>');
    echo ('</body>');
    echo ('</html>');
}
?>